<template>
  <div class="container">
    <h1>新建巡更项目</h1>
    <hr style="border: 1px solid rgb(240, 240, 240)" />
    <span>基础信息</span>
    <div class="body">
      <div class="input">
        <div class="input-left">
          <span>项目名称</span>
        </div>
        <div class="input-right">
          <el-input v-model="project_name" placeholder="请输入" />
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>添加人</span>
        </div>
        <div class="input-right">
          <el-input v-model="add_person" placeholder="请输入" />
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>状态</span>
        </div>
        <div class="input-right">
          <el-radio-group v-model="project_status">
            <el-radio value="启用">启用</el-radio>
            <el-radio value="停用">停用</el-radio>
          </el-radio-group>
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>巡更标准</span>
        </div>
        <div class="input-right">
          <el-input
            v-model="patrol_standard"
            type="textarea"
            :rows="5"
            placeholder="请输入"
          />
        </div>
      </div>
      <div style="margin: 2rem 0 2rem 12rem">
        <el-button type="primary" @click="addOrUpdate">提交</el-button>
        <el-button type="default" @click="to('patrol/project')">返回</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import to from "@/utils/to.js";
import axios from "axios";
import { ElMessage } from "element-plus";
import { useRoute } from "vue-router";

const route = useRoute();
const id = route.query.id;

const project_name = ref("");
const add_person = ref("");
const project_status = ref("");
const patrol_standard = ref("");

const addOrUpdate = () => {
  let config = {};

  if (project_name.value === "") {
    ElMessage({
      message: "请输入项目名称",
      type: "warning",
    });
    return;
  } else if (add_person.value === "") {
    ElMessage({
      message: "请输入添加人",
      type: "warning",
    });
    return;
  } else if (project_status.value === "") {
    ElMessage({
      message: "请选择项目状态",
      type: "warning",
    });
    return;
  }

  if (id === undefined || id === "") {
    config = {
      url: "http://localhost:8080/patrolProject/addPatrolProject",
      method: "POST",
      headers: {
        Authorization: localStorage.getItem("token"),
      },
      data: {
        projectName: project_name.value,
        projectStandard: patrol_standard.value,
        projectStatus: project_status.value,
        addPerson: add_person.value,
      },
    };
  } else {
    config = {
      url: "http://localhost:8080/patrolProject/updatePatrolProject",
      method: "PUT",
      headers: {
        Authorization: localStorage.getItem("token"),
      },
      data: {
        id: id,
        projectName: project_name.value,
        projectStandard: patrol_standard.value,
        projectStatus: project_status.value,
        addPerson: add_person.value,
      },
    };
  }

  const response = axios(config);
  response
    .then((data) => {
      if (data.data.code === 1) {
        ElMessage({
          message: data.data.msg,
          type: "success",
        });
        setTimeout(() => {
          to('patrol/project')
        }, 2000);
      } else {
        ElMessage({
          message: data.data.msg,
          type: "error",
        });
      }
    })
    .catch((error) => {
      console.log(error);
    });
};

const get = () => {
  const config = {
    url: "http://localhost:8080/patrolProject/selectPatrolProject",
    method: "GET",
    headers: {
      Authorization: localStorage.getItem("token"),
    },
    params: {
      id: id,
    },
  };

  const response = axios(config);
  response
    .then((data) => {
      project_name.value = data.data.data.projectName;
      add_person.value = data.data.data.addPerson;
      project_status.value = data.data.data.projectStatus;
      patrol_standard.value = data.data.data.projectStandard;
    })
    .catch((error) => {
      console.log(error);
    });
};

onMounted(() => {
  get();
});
</script>

<style scoped>
.container {
  background-color: white;
  height: auto;
  margin-top: 2rem;
  margin-bottom: 2rem;
}

h1 {
  font-size: 2rem;
  font-weight: 600;
  padding: 2rem 0 10px 4rem;
}

span {
  display: inline-block;
  font-size: 15px;
  margin: 1rem 2rem 1rem 0;
  line-height: 24px;
  width: 100px;
  text-align: right;
}

.body {
  display: grid;
  justify-content: center;
  align-items: center;
}

.input {
  display: flex;
  justify-content: center;
  align-items: center;
  width: auto;
}

.input-right {
  width: 300px;
}
</style>
